/*
 * Copyright (C) 2018 The Asiainfo-Ability Authors
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package org.asiainfo.ability.gateway.mq;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;

import java.util.List;

/**
 * @Author: visen
 * @Date: 2018/3/27
 * @Description:
 */
public class Listener {
    @KafkaListener(topics = "${tgs.kafka.topics}", containerFactory = "batchFactory")
    public void listen(List<ConsumerRecord<?, ?>> records, Acknowledgment ack) {

        /*
        List<B_ZDRYGK_ZDRYXX_FJ_HCB1> list = new ArrayList<B_ZDRYGK_ZDRYXX_FJ_HCB1>();
        ObjectMapper mapper = new ObjectMapper();
        B_ZDRYGK_ZDRYXX_FJ_HCB1 b_zdrygk_zdryxx_fj_hcb1 =null;
        for (ConsumerRecord<?, ?> record : records) {
            try {
                b_zdrygk_zdryxx_fj_hcb1 = mapper.readValue(record.value().toString(), B_ZDRYGK_ZDRYXX_FJ_HCB1.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (null != b_zdrygk_zdryxx_fj_hcb1) {
                list.add(b_zdrygk_zdryxx_fj_hcb1);
            }
        }
        try {
            List<B_ZDRYGK_ZDRYXX_FJ_HCB1> hcb1List = b_ZDRYGK_ZDRYXX_FJ_HCB1Repository.save(list);
            b_ZDRYGK_ZDRYXX_FJ_HCB1Repository.flush();
            logger.info("flush size: " + hcb1List.size());
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            logger.info("start commit offset");
            ack.acknowledge();//手动提交偏移量
            logger.info("stop commit offset");
        }
        */
    }

}
